package de.htwds.mada.todolistDB;

import java.sql.SQLException;
import java.util.List;
import java.util.ListIterator;

import de.htwds.mada.R;
import de.htwds.mada.todolistDB.db.DatabaseHelper;
import de.htwds.mada.todolistDB.db.Repository;
import de.htwds.mada.todolistDB.model.Category;
import de.htwds.mada.todolistDB.model.Todo;
import de.htwds.mada.todolistDB.model.TodoCategory;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class TODODBTodoCatActivity extends Activity  {
	
	private static Repository db;
	private static MadaArrayAdapter<TodoCategory> adapter;
	private static MadaArrayAdapter<Category> dialogAdapter;
	
	private Todo todo = null;
	private TodoCategory tc = null;
	
	private Button btnAdd;
	private Button btnRemove;
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tododb_todocat);
		
		if (dialogAdapter == null)
			dialogAdapter = new MadaArrayAdapter<Category>(this);
		
		if (adapter == null)
			adapter = new MadaArrayAdapter<TodoCategory>(this);
		
		if (db == null)
			db = new Repository(new DatabaseHelper(this));
		
		btnAdd = (Button) findViewById(R.id.btn_todocat_add);
		btnRemove = (Button) findViewById(R.id.btn_todocat_remove);
		listView = (ListView) findViewById(R.id.listView_todocat);
		
		listView.setAdapter(adapter);
		
		todo = (Todo) getIntent().getParcelableExtra("object");
		
		dbZugriff();
		readCategories();
		
		btnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub	
				AlertDialog dialog = createDialog();
				dialog.show();
			}
		});
		
		btnRemove.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (tc != null)
					try {
						db.deleteTodoCategory(tc);
						tc = null;
						Toast.makeText(TODODBTodoCatActivity.this, R.string.db_entry_deleted, Toast.LENGTH_SHORT).show();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						Toast.makeText(TODODBTodoCatActivity.this, R.string.error_db_write, Toast.LENGTH_SHORT).show();
						Log.e("TodoCat", e.getMessage());
					}
				dbZugriff();
				
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				tc = (TodoCategory) listView.getItemAtPosition(position);
			}
		});
		
	}
	
	/**
	 * 
	 */
	private void dbZugriff(){
		adapter.clear();
		
		Log.d("TodoCategory-Zugriff", "Zugriffs-Methode ");
		
		List<TodoCategory> list = null;
		if (todo != null) {
			try {
				list = db.getTodoCategories(todo); //Datenbank-Zugriff
				Log.d("TodoCategory-Zugriff", "Laenge: " + list.size());
			} catch (SQLException e) {
				e.printStackTrace();
				Toast.makeText(this, "Fehler bei Datenbank-Zugriff", Toast.LENGTH_SHORT).show();
			}
			
			ListIterator<TodoCategory> iterator = list.listIterator();
			while(iterator.hasNext()){
				TodoCategory todoCat = iterator.next();
				Log.d("TodoCategory-Zugriff", "gelesen: " + todoCat.toString());
				adapter.add(todoCat);
			}
		}
		
		adapter.notifyDataSetChanged();
	}
	
	/**
	 * 
	 * @return
	 */
	private AlertDialog createDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.todo_db_dialog);
		builder.setAdapter(dialogAdapter, new Dialog.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Category c = dialogAdapter.getItem(which);
				tc = new TodoCategory(todo, c);
				Log.d("Dialog", "TodoCat "+tc.toStringComplete());
				try {
					db.createOrUpdateTodoCategory(tc);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(TODODBTodoCatActivity.this, "Fehler bei Datenbank-Zugriff", Toast.LENGTH_SHORT).show();
				}
				dbZugriff();				
			}
		});
		return builder.create();		
	}
	
	/**
	 * 
	 */
	private void readCategories() {
		dialogAdapter.clear();
		
		List<Category> list = null;
		try {
			list = db.getCategories(); //Datenbank-Zugriff
			Log.d("Category-Zugriff", "Laenge: " + list.size());
		} catch (SQLException e) {
			e.printStackTrace();
			Toast.makeText(this, "Fehler bei Datenbank-Zugriff", Toast.LENGTH_SHORT).show();
		}
		
		ListIterator<Category> iterator = list.listIterator();
		while(iterator.hasNext()){
			Category cat = iterator.next();
			Log.d("Cattegory-Zugriff", "gelesen: " + cat.toString());
			dialogAdapter.add(cat);
		}
		
		dialogAdapter.notifyDataSetChanged();
	}

}
